package com.woniu.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.lang.Snowflake;
import com.github.pagehelper.PageInfo;
import com.woniu.entity.FlowerInfo;
import com.woniu.entity.FlowerType;
import com.woniu.service.FlowerInfoService;
import com.woniu.util.JsonResult;
import com.woniu.vo.FlowerInfoVo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @program:
 * @ClassName:
 * @description:
 * @author: zgc
 * @date:
 * @Version 1.0
 **/
@RestController
@RequestMapping("/flowerInfo")
public class FlowerInfoController {
    @Resource
    private FlowerInfoService flowerInfoService;
    @Resource
    private Snowflake snowflake;
    
    // 查询所有鲜花商品的信息
    @RequestMapping("/selectAllFlowerInfo")
    public JsonResult<PageInfo<FlowerInfoVo>> selectAllFlowerInfo(Integer pageNum, Integer pageSize,String flowerName,Long startTime,Long endTime,Integer isDelete){
         PageInfo<FlowerInfoVo> list= flowerInfoService.selectAllFlowerInfo(pageNum,pageSize,flowerName,startTime,endTime,isDelete);
        return JsonResult.ok(200, list);
    }
    
    //删除、上架、下架商品操作
    @RequestMapping("/updateFlowerInfoByFlowerIdToStatus")
    public JsonResult<Integer> updateFlowerInfoByFlowerIdToStatus(Integer flowerId, Date publisherDate, Byte isDelete){
        if (isDelete==0){publisherDate = new DateTime();}
        FlowerInfo flowerInfo = new FlowerInfo(flowerId,publisherDate,isDelete);
        int i = flowerInfoService.updateFlowerInfoByFlowerIdToStatus(flowerInfo);
        return JsonResult.build(200,"操作成功",i);
    }
    
    //查询所有待上架的商品信息
    @RequestMapping("/selectAllFlowerInfoByIsDelete")
    public JsonResult<List<FlowerInfo>> selectAllFlowerInfoByIsDelete(Byte isDelete){
        FlowerInfo flowerInfo = new FlowerInfo(isDelete);
        List<FlowerInfo> list = flowerInfoService.selectAllFlowerInfoByIsDelete(flowerInfo);
        return JsonResult.ok(200, list);
    }
    
    //图片上传功能
    @RequestMapping("/upload")
    public String uploadFile(@RequestParam("file") MultipartFile file) {
        String fileName = file.getOriginalFilename();
        // 检查文件名中是否包含点
        int dotIndex = fileName.lastIndexOf('.');
        // 如果没有点或者点是文件名的第一个字符，则返回空字符串
        if (dotIndex <= 0) {
            return "";
        }
        // 返回点之后的部分作为文件后缀 +雪花id
        String newfileName =snowflake.nextId() + fileName.substring(dotIndex);
        File dest = new File("D:\\Program Files\\nginx-1.26.1\\html\\image\\Test" ,newfileName);
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return newfileName;
    }
    
    // 添加商品功能
    @RequestMapping("/addFlowerInfo")
    public JsonResult<Integer> addFlowerInfo(@RequestBody FlowerInfo flowerInfo){
        if (flowerInfo.getFlowerId()!=null){
            flowerInfo.setIsDelete(null);
            int i = flowerInfoService.updateFlowerInfo(flowerInfo);
            return JsonResult.build(200,"操作商品成功",i);
        }else {
            flowerInfo.setCreateTime(new DateTime());
            int i = flowerInfoService.addFlowerInfo(flowerInfo);
            return JsonResult.build(200,"操作商品成功",i);
        }
        
    }
    
    // 查询所有商品分类
    @RequestMapping("/selectAllFlowerType")
    public JsonResult<List<FlowerType>> selectAllFlowerType(){
        List<FlowerType> list = flowerInfoService.selectAllFlowerType();
        return JsonResult.ok(200, list);
    }
    
    
    
}
